//#define CREATE_DB_TASK
namespace Flow.Tasks.Data.Migrations
{
    using Flow.Tasks.Contract.Message;
    using Flow.Tasks.Data.Core;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Flow.Tasks.Data.FlowTasksEntities>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(Flow.Tasks.Data.FlowTasksEntities context)
        {
#if CREATE_DB_TASK
            TraceEvent teDebug = CreateEvent(TraceEventType.Debug, "Debug");
            TraceEvent teActivity = CreateEvent(TraceEventType.Activity, "Activity");
            TraceEvent teInfo = CreateEvent(TraceEventType.Info, "Info");
            TraceEvent teError = CreateEvent(TraceEventType.Error, "Error");

            WorkflowStatus wsInProgress = CreateStatus(WorkflowStatusType.InProgress, "InProgress");
            WorkflowStatus wsCompleted = CreateStatus(WorkflowStatusType.Completed, "Completed");
            WorkflowStatus wsAborted = CreateStatus(WorkflowStatusType.Aborted, "Aborted");
            WorkflowStatus wsTerminated = CreateStatus(WorkflowStatusType.Terminated, "Terminated");

            SketchStatus skSaved = CreateSketchStatus("Saved", "Sketch Saved");
            SketchStatus skDeployedDev = CreateSketchStatus("DeployedDev", "Deployed to dev");
            SketchStatus skDeployedProd = CreateSketchStatus("DeployedProd", "Deployed to prod");
            SketchStatus skSentToSketch = CreateSketchStatus("SentToSketch", "Sent To Sketch");
            SketchStatus skAborted = CreateSketchStatus("Aborted", "Abort workflow deployment");

            WorkflowCode wc1 = CreateWorkflowCode("SampleWf1", "this is a sample wf code for testing (1)");
            WorkflowConfiguration wfc1 = CreateWorkflowConfiguration(wc1, "BasicHttpBinding_FlowTasks", "", "");

            WorkflowCode wc2 = CreateWorkflowCode("SampleWf2", "this is a sample wf code for testing (2)");
            WorkflowConfiguration wfc2 = CreateWorkflowConfiguration(wc2, "BasicHttpBinding_IFlowTasksOperations2", "http://localhost/Flow.Tasks.Workflows/SampleWf2.xamlx", "BasicHttpBinding_FlowTasks");

            WorkflowCode wc3 = CreateWorkflowCode("SampleWf3", "this is a sample wf code for testing (3)");
            WorkflowConfiguration wfc3 = CreateWorkflowConfiguration(wc3, "BasicHttpBinding_IFlowTasksOperations3", "http://localhost/Flow.Tasks.Workflows/SampleWf3.xamlx", "BasicHttpBinding_FlowTasks");

            WorkflowCode wc4 = CreateWorkflowCode("SampleWf4", "this is a sample wf code for testing (4)");
            WorkflowConfiguration wfc4 = CreateWorkflowConfiguration(wc4, "BasicHttpBinding_IFlowTasksOperations4", "http://localhost/Flow.Tasks.Workflows/SampleWf4.xamlx", "BasicHttpBinding_FlowTasks");

            WorkflowCode wc4a = CreateWorkflowCode("SampleWf4a", "this is a sample wf code for testing (4a)");
            WorkflowConfiguration wfc4a = CreateWorkflowConfiguration(wc4a, "BasicHttpBinding_IFlowTasksOperations4a", "http://localhost/Flow.Tasks.Workflows/SampleWf4a.xamlx", "BasicHttpBinding_FlowTasks");

            WorkflowCode wc5 = CreateWorkflowCode("SampleWf5", "this is a sample wf code for testing (5)");
            WorkflowConfiguration wfc5 = CreateWorkflowConfiguration(wc5, "BasicHttpBinding_IFlowTasksOperations5", "http://localhost/Flow.Tasks.Workflows/SampleWf5.xamlx", "BasicHttpBinding_FlowTasks");

            WorkflowCode wc6 = CreateWorkflowCode("SampleWf6", "this is a sample wf code for testing (6)");
            WorkflowConfiguration wfc6 = CreateWorkflowConfiguration(wc6, "BasicHttpBinding_IFlowTasksOperations6", "http://localhost/Flow.Tasks.Workflows/SampleWf6.xamlx", "BasicHttpBinding_FlowTasks");

            WorkflowCode wc7 = CreateWorkflowCode("SampleWf7", "this is a sample wf code for testing (7)");
            WorkflowConfiguration wfc7 = CreateWorkflowConfiguration(wc7, "BasicHttpBinding_IFlowTasksOperations7", "http://localhost/Flow.Tasks.Workflows/SampleWf7.xamlx", "BasicHttpBinding_FlowTasks");

            WorkflowCode wc8 = CreateWorkflowCode("SampleWf8", "this is a sample wf code for testing (8)");
            WorkflowConfiguration wfc8 = CreateWorkflowConfiguration(wc8, "BasicHttpBinding_IFlowTasksOperations8", "http://localhost/ServiceWorkflowsVB/SampleWf8.xamlx", "BasicHttpBinding_FlowTasks");

            WorkflowCode wc9 = CreateWorkflowCode("SampleWf9", "this is a sample wf code for testing (9)");
            WorkflowConfiguration wfc9 = CreateWorkflowConfiguration(wc9, "BasicHttpBinding_IFlowTasksOperations9", "http://localhost/ServiceWorkflowsVB/SampleWf9.xamlx", "BasicHttpBinding_FlowTasks");

            // Topic
            TopicStatus topicNew = CreateTopicStatus("New", "New topic message");
            TopicStatus topicRead = CreateTopicStatus("Read", "Read topic message");

            // Holiday
            Holiday h2014 = CreateHoliday(2014,
                                          "01/01/2014,27/01/2014,25/04/2014,09/06/2014,06/10/2014,25/12/2014,26/12/2014");
            Holiday h2015 = CreateHoliday(2015,
                                          "01/01/2015,26/01/2015,25/04/2015,08/06/2015,05/10/2015,25/12/2015,28/12/2015");
            Holiday h2016 = CreateHoliday(2016,
                                          "01/01/2016,26/01/2016,25/04/2016,13/06/2016,03/10/2016,26/12/2016,27/12/2016");

            context.TraceEvents.AddOrUpdate(
                te => te.Type,
                teDebug,
                teActivity,
                teError,
                teInfo
                );

            context.WorkflowStatuses.AddOrUpdate(
                ws => ws.Status,
                wsCompleted,
                wsAborted,
                wsInProgress,
                wsTerminated
                );

            context.SketchStatuses.AddOrUpdate(
                sk => sk.Status,
                skAborted,
                skDeployedDev,
                skDeployedProd,
                skSaved,
                skSentToSketch
                );

            context.TopicStatuses.AddOrUpdate(
                t => t.Status,
                topicNew,
                topicRead
                );

            //context.WorkflowCodes.AddOrUpdate(
            //    wc => wc.Code,
            //    wc1, wc2, wc3, wc4, wc5, wc6, wc7, wc8, wc9);

            context.WorkflowConfigurations.AddOrUpdate(
                wfc => wfc.ServiceEndpoint,
                wfc1, wfc2, wfc3, wfc4, wfc4a, wfc5, wfc6, wfc7, wfc8, wfc9);

            context.Holidays.AddOrUpdate(
                h => h.HolidayId,
                h2014, h2015, h2016
                );
#endif
        }

        private TopicAttachment CreateTopicAttachment(string file, Guid oid, TopicMessage m)
        {
            return new TopicAttachment { FileName = file, OidDocument = oid, TopicMessage = m };
        }

        private TopicUser CreateTopicUser(string user, TopicMessage msg, TopicStatus topicRead)
        {
            return new TopicUser { User = user, TopicMessage = msg, TopicStatus = topicRead };
        }

        private TopicStatus CreateTopicStatus(string status, string description)
        {
            return new TopicStatus { Status = status, Description = description };
        }

        private TopicMessage CreateMessage(string msg, string from, string to, DateTime when, Boolean isTopic, Topic t)
        {
            return new TopicMessage { Message = msg, From = from, To = to, When = when, IsTopic = isTopic, Topic = t };
        }

        private Topic CreateTopic(string title)
        {
            return new Topic { Title = title };
        }

        private TaskUserHandOver CreateTaskUserHandOver(TaskDefinition taskDefinition, string user)
        {
            return new TaskUserHandOver
            {
                TaskDefinition = taskDefinition,
                InUse = false,
                User = user
            };
        }

        private TaskUser CreateTaskUser(TaskDefinition taskDefinition, string user)
        {
            return new TaskUser
            {
                TaskDefinition = taskDefinition,
                InUse = false,
                User = user
            };
        }

        private TaskDefinition CreateTaskDefinition(WorkflowDefinition workflowDefinition, string title, string desc, string uiCode)
        {
            return new TaskDefinition
            {
                Description = desc,
                Title = title,
                UiCode = uiCode,
                WorkflowDefinition = workflowDefinition,
                TaskOid = Guid.NewGuid()
            };
        }

        private TaskConfiguration CreateTaskConfiguration(WorkflowCode workflowCode, string taskCode)
        {
            return new TaskConfiguration
            {
                Description = "New Description",
                Title = "New titile",
                TaskCode = taskCode,
                WorkflowCode = workflowCode
            };
        }

        private WorkflowTrace CreateWorkflowTrace(TraceEvent traceEvent, WorkflowDefinition workflowDefinition,
            string message, DateTime when, string user)
        {
            return new WorkflowTrace
            {
                TraceEvent = traceEvent,
                WorkflowDefinition = workflowDefinition,
                Message = message,
                User = user,
                When = when
            };
        }

        private WorkflowDefinition CreateWorkflowDefinition(WorkflowCode workflowCode, WorkflowStatus workflowStatus)
        {
            return new WorkflowDefinition
            {
                WorkflowOid = Guid.NewGuid(),
                WorkflowCode = workflowCode,
                StartedOn = DateTime.Now,
                WorkflowStatus = workflowStatus
            };
        }

        private WorkflowProperty CreateWorkflowProperty(Property property, WorkflowCode workflowCode)
        {
            return new WorkflowProperty
            {
                Property = property,
                WorkflowCode = workflowCode
            };
        }

        private Property CreateProperty(string name, string value)
        {
            return new Property
            {
                Name = name,
                Value = value,
                Type = "S"
            };
        }

        private TraceEvent CreateEvent(TraceEventType type, string desc)
        {
            return new TraceEvent
            {
                Type = type.ToString(),
                Description = desc
            };
        }

        private WorkflowStatus CreateStatus(WorkflowStatusType status, string desc)
        {
            return new WorkflowStatus
            {
                Status = status.ToString(),
                Description = desc
            };
        }

        private SketchStatus CreateSketchStatus(string status, string desc)
        {
            return new SketchStatus
            {
                Status = status,
                Description = desc
            };
        }

        private WorkflowCode CreateWorkflowCode(string code, string desc)
        {
            return new WorkflowCode
            {
                Code = code,
                Description = desc
            };
        }

        private WorkflowConfiguration CreateWorkflowConfiguration(WorkflowCode workflowCode, string serviceEndpoint, string serviceUrl, string bindingConfiguration)
        {
            return new WorkflowConfiguration
            {
                EffectiveDate = DateTime.Now,
                ServiceUrl = serviceUrl,
                ServiceEndpoint = serviceEndpoint,
                BindingConfiguration = bindingConfiguration,
                WorkflowCode = workflowCode
                //ServiceDefinition = LoadWorkflow(workflowCode.Code)
            };
        }

        //private string LoadWorkflow(string code)
        //{
        //    var path = @"..\..\..\Flow.Workflows.Test\ServiceWorkflows\";

        //    var file = Path.Combine(path, code + ".xamlx");
        //    return File.ReadAllText(file);
        //}
    }
}
